<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Articles extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    //INSERTING DATA//

    function insert($input) {
        $this->db->set('title', $input['title']);
        $this->db->set('author_id', $input['author_id']);
        $this->db->set('content', $input['content']);
        $this->db->set('category_id', $input['category_id'], FALSE);
        $this->db->set('post_date', date('Y-m-d'));
        $this->db->set('published', $input['published'], FALSE);
        $this->db->insert('article');
        //check image
        if (isset($input['image'])) {
            $article_id = $this->db->insert_id();
            $this->db->flush_cache();
            $this->db->set('article_id', $article_id);
            $this->db->set('image_url', $input['image']['file_name']);
            $this->db->set('image_width', "{$input['image']['image_width']}");
            $this->db->set('image_height', "{$input['image']['image_height']}");
            $this->db->set('image_title', $input['image_title']);
            $this->db->insert('image');
            $this->create_front_thumbnail($input['image']);
        }
    }

    //SELECTING DATA//

    function get_article($atribute, $value, $join = false) {
        if ($join) {
            $this->db->join('user', 'user.user_id = article.author_id');
            $this->db->join('category', 'article.category_id = category.category_id');
        }
        $this->db->where($atribute, $value);
        return $this->db->get('article');
    }

    function get_all($join = false) {
        if ($join) {
            $this->db->join('user', 'user.user_id = article.author_id');
            $this->db->join('category', 'article.category_id = category.category_id');
        }
        return $this->db->get('article');
    }

    function get_all_published($join = false,$limit = null,$offset = 0) {
        $this->db->where('published', 1);
        $this->db->order_by('article_id', 'desc');
        if ($join) {
            $this->db->join('user', 'user.user_id = article.author_id');
            $this->db->join('category', 'article.category_id = category.category_id');
            $this->db->where('category_published', 1);
        }
        if($limit != null){
            $query = $this->db->get('article',$limit,$offset);
        } else {
            $query = $this->db->get('article');
        }
        $result = array();
        $count = 0;
        foreach ($query->result() as $row) {
            $result[$count] = $row;
            $result[$count]->content = $this->get_front_content($result[$count]->content);
            $result[$count]->image = $this->get_image('article_id', $row->article_id);
            $count++;
        }
        return $result;
    }

    function get_article_by_id($id,$join = false) {
        $this->db->where('article_id', $id);
        if($join){
            $this->db->join('category','category.category_id = article.category_id');
            $this->db->join('user','user.user_id = article.author_id');
        }
        $query = $this->db->get('article');
        return ($query->num_rows() > 0) ? $query->first_row() : null;
    }
    
    function get_article_by_image_id($image_id) {
        $this->db->where('image_id', $image_id);
        $this->db->join('image','article.article_id = image.article_id');
        $query = $this->db->get('article');
        return ($query->num_rows() > 0) ? $query->first_row() : null;
    }

    function get_top_post($count = 5) {
        $this->db->select('article_id,title,viewed');
        $this->db->order_by('viewed', 'desc');
        $this->db->where('published', 1);
        $this->db->limit($count);
        return $this->db->get('article');
    }
    
    function get_article_by_category_name($name,$count = 5){
        $this->db->join('category','category.category_id = article.category_id');
        $this->db->where('category_name', $name);
        $this->db->limit($count);
        return $this->db->get('article');
    }

    //UPDATING DATA//

    function update_by_id($id, $input) {
        $this->db->where('article_id', $id);
        $this->db->set('title', $input['title']);
        $this->db->set('content', $input['content']);
        $this->db->set('category_id', $input['category_id'], FALSE);
        $this->db->set('published', $input['published'], FALSE);
        $this->db->update('article');
        //check image
        if (isset($input['image'])) {
            $this->db->flush_cache();
            $this->db->set('article_id', $id);
            $this->db->set('image_url', $input['image']['file_name']);
            $this->db->set('image_width', "{$input['image']['image_width']}");
            $this->db->set('image_height', "{$input['image']['image_height']}");
            $this->db->set('image_title', $input['image_title']);
            $this->db->insert('image');
            $this->create_front_thumbnail($input['image']);
        }
    }
    
    function update_viewed_by_id($id,$value){
        $this->db->set('viewed', $value);
        $this->db->where('article_id', $id);
        $this->db->update('article');
    }

    //DELETING DATA//

    function delete_by_id($id) {
        $this->db->where('article_id', $id);
        $this->db->delete('article');
        $this->db->flush_cache();

        //deleting image
        $this->db->where('article_id', $id);
        $query = $this->db->get('image');
        foreach ($query->result() as $row) {
            $this->config->load('upload');
            $path = $this->config->item('upload_path') . $row->image_url;
            unlink($path);
        }
        $this->db->flush_cache();
        $this->db->where('article_id', $id);
        $this->db->delete('image');
    }
    
    //CONTENT MANIPULATION//
    function get_front_content($content){
        $result = strip_tags($content, '<p>');
        if (strlen($result) < 280) {
            return $result;
        } else {
            $pos = stripos($result, '</p>');
            if (($pos === false) || ($pos > 280)) {
                $count = 280;
                $len = strlen($result);
                while((stripos("., ?!",''.$result{$count}) === false) && ($count < $len)){
                    $count++;
                }
                $result =  substr($result, 0, $count);
                return ($pos === false) ? $result.'...' : $result.'...</p>';
            } else {
                return substr($result, 0, $pos);
            }
        }
    }

    //IMAGE MANIPULATION// 
    
    function get_image_by_id($image_id){
        $this->db->where('image_id',$image_id);
        $query = $this->db->get('image');
        return $query->first_row();
    }

    function get_image($atribute, $value) {
        $this->db->where($atribute, $value);
        $this->db->order_by('image_id','asc');
        return $this->db->get('image');
    }
    
    function update_image_by_id($image_id,$input){
        $this->db->where('image_id',$image_id);
        $this->db->set('image_title', $input['image_title']);
        $this->db->update('image');
    }
    
    function delete_image_by_id($image_id){
        $this->db->where('image_id', $image_id);
        $query = $this->db->get('image');
        $result = $query->first_row();
        //delete image
        $this->config->load('upload');
        $path = $this->config->item('upload_path');
        unlink($path.$result->image_url);
        unlink($path.'thumb/'.$result->image_url);
        //delete on table
        $this->db->flush_cache();
        $this->db->where('image_id', $image_id);
        $this->db->delete('image');
    }

    function create_front_thumbnail($image_data) {
        $this->config->load('upload');
        $config['image_library'] = 'GD2';
        $config['source_image'] = $this->config->item('upload_path').$image_data['file_name'];
        $config['new_image'] = $this->config->item('upload_path').'thumb/'.$image_data['file_name'];
        $config['maintain_ratio'] = TRUE;
        if (($image_data['image_width'] / $image_data['image_height']) > (3 / 2)) {
            $width = (180 * $image_data['image_width']) / $image_data['image_height'];
            $height = 180;
        } else {
            $height = (270 * $image_data['image_height']) / $image_data['image_width'];
            $width = 270;
        }
        $config['width'] = $width;
        $config['height'] = $height;
        $this->load->library('image_lib', $config);
        if (!$this->image_lib->resize()) {
            echo $this->image_lib->display_errors();
        }
    }

}